package org.bugjail.servlet.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bugjail.data.Person;
import org.bugjail.dataaccess.DAOFactory;
import org.bugjail.servlet.BasicServlet;

/**
 * Servlet implementation class for Servlet: EditSuiteServlet
 *
 */
 public class EditCurrentUserServlet extends BasicServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public EditCurrentUserServlet() {
		super();
	}   	

	@Override
	protected void action(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Person user = getLoggedUser();
		user.setName(request.getParameter("name"));
		user.setMail(request.getParameter("mail"));
		if (request.getParameter("pass") != null) {
			if (!request.getParameter("pass").equalsIgnoreCase("")) {
				if (request.getParameter("pass").equalsIgnoreCase(request.getParameter("passconfirm"))) {
					user.setPassword(request.getParameter("pass"));
				} else {
					throw new RuntimeException("Password confirmation is invalid");
				}
			}
		}
		DAOFactory.getDAOFactory().getPersonDAO().update(user);
		response.sendRedirect("HomeServlet");
	}

	@Override
	protected void beforeAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}   	  	    
}